package cn.com.yang.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Author 小米电脑
 * @Date 2020/7/26 20:06
 */
public class TestListThread {

    /**
     * 生成数据
     *
     * @param n
     * @return
     */
    public static List<String> creatDatas(int n) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i <= n; i++) {
            list.add(i + "");
        }
        return list;
    }


    /**
     * 线程处理
     *
     * @param threadSize
     * @param list
     * @throws InterruptedException
     */
    public static void doThread(int threadSize, List<String> list) throws InterruptedException {
        Long start = System.currentTimeMillis();
        int dataSize = list.size();
        //线程数
        int threadNum = dataSize / threadSize + 1;
        //定义标志
        boolean special = dataSize % threadSize == 0;
        //创建线程池
        ExecutorService exec = Executors.newFixedThreadPool(threadNum);
        //定义一个任务集合
        List<Callable<Integer>> tasks = new ArrayList<>();

        Callable<Integer> task = null;

        List<String> currList = new ArrayList<>();
        //确定每条线程的数据
        for (int i = 0; i < threadNum; i++) {
            if (i == threadNum - 1) {
                if (special) {
                    break;
                }
                currList = list.subList(threadSize * i, dataSize);

            } else {
                currList = list.subList(threadSize * i, threadSize * (i + 1));
            }
            //list第几组
            System.out.println("第" + (i + 1) + "组：" + currList.toString());
            final List<String> listStr = currList;

            //单个线程逻辑处理
            task = new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    //listStr.add("***");//Caused by: java.util.ConcurrentModificationException
                    System.out.println(Thread.currentThread().getName() + "线程：" + listStr);
                    return 1;
                }
            };
            //这里提交的任务容器列表和返回的Future列表存在对应的关系
            tasks.add(task);
        }
        List<Future<Integer>> results = exec.invokeAll(tasks);

        for (Future<Integer> future : results) {
            try {

                future.get();
            } catch (ExecutionException e) {
                e.printStackTrace();
                //重置当前线程的中断位置 state为false,便于该线程以后被其他任务正常调用
                Thread.interrupted();
            }
        }
        //36523毫秒
        //关闭线程池
        exec.shutdown();
        System.out.println("==线程任务执行结束==");
        Long end = System.currentTimeMillis();
        System.out.println("==执行任务消耗了:" + (end - start) + "毫秒==");
    }

    public static void main(String[] args) {
        // 10000000
        // ==线程任务执行结束==
        //==执行任务消耗了:7855毫秒==
        List<String> list = creatDatas(10000000);
        try {
            //ctrl + alt + t 快速找到选项
            doThread(200, list);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        long s = System.currentTimeMillis();
//        List<String> list = creatDatas(10000000);
//        list.add("***");
//        long e = System.currentTimeMillis();
//        System.out.println("==执行任务消耗了:" + (e - s) + "毫秒==");
//    }

}
